//
//  UMainSectionModel.m
//  s.
//
//  Created by fanshijian on 2022/8/24.
//  Copyright © 2022 J. All rights reserved.
//

#import "MainSectionModel.h"
#import "BaseModel.h"

@implementation MainSectionModel

+ (instancetype)sectionWithHeaderModel:(__kindof BaseModel *)model{
    return [self sectionWithHeader:model footer:nil];
}
+ (instancetype)sectionWithHeader:(__kindof BaseModel *)head footer:(__kindof BaseModel *)footer{
    MainSectionModel *m = MainSectionModel.new;
    m.h_model             = head;
    m.f_model             = footer;
    return m;
}

- (instancetype)init {
    if (self = [super init]) {
        self.layoutAlignLeft = NO;
    }
    return self;
}

- (NSMutableArray *)items{
    if (!_items)_items = NSMutableArray.array;
    return _items;
}

- (NSUInteger)rowCount{
    return _items.count;
}

- (BaseModel *)getHeaderObj {
    MainSectionModel *s = self;
    BaseModel *obj = s.h_modelBlock ? s.h_modelBlock(s) : s.h_model;
    return obj;
}

- (void)sectionInsets:(UIEdgeInsets)insets ls:(CGFloat)ls is:(CGFloat)is {
    _sectionInsets = insets;
    _lineSpacing   = ls;
    _itemSpacing   = is;
}

#pragma mark - CHGCollectionViewSupplementaryElementModelProtocol
- (NSString*)reusableViewInCollectionView:(UICollectionView*)collectionView supplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        MainSectionModel *s = self;
        BaseModel *obj = s.h_modelBlock ? s.h_modelBlock(s) : s.h_model;
        return obj.idf?obj.idf:@"BEBaseReusableView";
    } else {
        MainSectionModel *s = self;
        BaseModel *obj = s.f_modelBlock ? s.f_modelBlock(s) : s.f_model;
        return obj.idf?obj.idf:@"BEBaseReusableView";
    }
}

///动态设置某个分区头视图大小
- (CGSize)chg_collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    do {
        MainSectionModel *s = self;
        if (!s) break;
        BaseModel *obj = s.h_modelBlock ? s.h_modelBlock(s) : s.h_model;
        return obj ? obj.layout.itemSize : CGSizeZero;
    } while (0);
    return CGSizeZero;
}

//动态设置某个分区尾视图大小
- (CGSize)chg_collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    MainSectionModel *s = self;
    BaseModel *obj = s.f_modelBlock ? s.f_modelBlock(s) : s.f_model;
    return obj ? obj.layout.itemSize : CGSizeZero;
}

///动态设置每个分区的EdgeInsets 不包括header和footer
- (UIEdgeInsets)chg_collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    MainSectionModel *s = self;
    if (s.sectionInsetsBlock) {
        return s.sectionInsetsBlock(s);
    }else{
        return s.sectionInsets;
    }
}

//动态设置每行的间距大小
- (CGFloat)chg_collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return self.lineSpacing;
}

//动态设置每列的间距大小
- (CGFloat)chg_collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return self.itemSpacing;
}

#pragma mark - CHGTableViewHeaderFooterModelProtocol

/**
 绑定一个cell、headerFooter 类
 
 @return 返回类名
 */
-(NSString*)headerFooterClassInTableView:(UITableView*)tableView section:(NSInteger)section type:(CHGAdapterViewType)type {
    MainSectionModel *s = self;
    BaseModel *obj = s.h_modelBlock ? s.h_modelBlock(s) : s.h_model;
    return obj.idf?obj.idf:@"BEBaseHeaderFooterView";
}

/**
 返回当前headerFooter的高度
 
 @return headerFooter的高度
 */
-(CGFloat)headerFooterHeighInTableView:(UITableView*)tableView section:(NSInteger)section type:(CHGAdapterViewType)type {
    if (type == CHGAdapterViewTypeHeaderType) {
        MainSectionModel *s = self;
        BaseModel *obj = s.h_modelBlock ? s.h_modelBlock(s) : s.h_model;
        return obj ? obj.layout.itemSize.height : 0;
    }else {
        MainSectionModel *s = self;
        BaseModel *obj = s.f_modelBlock ? s.f_modelBlock(s) : s.f_model;
        return obj ? obj.layout.itemSize.height : 0;
    }
    return 0;
}

@end
